import axios from 'axios'
import { tokenKey } from "./constants.js"
import store from "../store/index"

axios.defaults.timeout = 1 * 60 * 1000;
axios.defaults.baseURL = "http://192.168.13.5:8092/";

axios.interceptors.request.use((config)=>{
	store.dispatch('showLoading');	
	var token=window.localStorage.getItem(tokenKey);
	if (token) {  			
		config.headers["Access-Token"] = token;
		config.headers["Content-Type"]="application/json";
	}
	return config;
},(error)=>{	
	store.dispatch('hideLoading');
	return Promise.reject(error);
});

axios.interceptors.response.use(function(response) { //请求回来
	store.dispatch('hideLoading');	
	return response;
}, function(error) {
	store.dispatch('hideLoading');
	if(error.message==="Request failed with status code 401"){
		alert("身份验证失败，请重新登录");
		window.location.href="/login"
	}
	return Promise.reject(error);
});

export default axios;

